<template>
  <ych-modal
    :fullscreen="false"
    size="middle"
    :title="title"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">

    <a-form-model ref="ruleForm" :model="model" :rules="rules">
      <a-row>
        <a-col :span="24">
          <a-form-model-item :label="$t('m.components.label.help_name')" prop="helpName" :label-col="colLayout2.fixed.labelCol" :wrapper-col="colLayout2.fixed.wrapperCol">
            <a-input v-model="model.helpName"></a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item :label="$t('m.components.label.help_desc')" prop="helpDesc" :label-col="colLayout2.fixed.labelCol" :wrapper-col="colLayout2.fixed.wrapperCol">
            <a-textarea v-model="model.helpDesc" :auto-size="{ minRows: 3 }"></a-textarea>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item :label="$t('m.common.label.sort')" prop="orderNum" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <a-input-number v-model="model.orderNum" :min="1" :precision="0" :step="1" style="width: 100%;"></a-input-number>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item :label="$t('m.components.label.help_is_global')" prop="isGlobal" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
            <a-switch v-model="isGlobal" @change="changeIsGlobal"/>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>

  </ych-modal>
</template>

<script>

import {GardLayoutMixin} from '@/mixins/GardLayoutMixin'
import {postAction} from '@/api/manage'
import YchConstant from "@/api/YchConstant";

export default {
  name: 'YchHelpUtil',
  mixins: [GardLayoutMixin],
  data() {
    return {
      visible: false,
      title: this.$t('m.components.label.tian-jia-wei-shi-li'),
      rules: {
        helpName: [{required: true, message: this.$t('m.components.label.required_message')}],
      },
      model: {
        helpName: '',
        helpDesc: '',
        orderNum: 1,
        isGlobal: 'N',
        scriptContent: '',
        helpCode: '',
      },
      isGlobal: false,
      url: {
        save_url: YchConstant.PREFIX_EFORM + '/manage/actAppHelp/save',
        checkMd5_url: YchConstant.PREFIX_EFORM + '/manage/actAppHelp/checkMd5',
      }
    }
  },
  methods: {
    handleCancel() {
      this.visible = false
    },
    changeIsGlobal(checked) {
      this.model.isGlobal = checked ? 'Y' : 'N'
    },
    async handleOk() {
      const valid = await this.$refs.ruleForm.validate();
      if (!valid) {
        return
      }
      const res = await postAction(this.url.save_url, this.model)
      if (res.success) {
        this.$message.success(res.message)
        this.handleCancel();
        this.$emit("ok")
      } else {
        this.$message.warning(res.message)
      }
    },
    edit(item) {
      this.model = {
        id: item.id,
        helpName: item.helpName,
        helpDesc: item.helpDesc,
        scriptContent: item.scriptContent,
        helpCode: item.helpCode,
        orderNum: item.orderNum,
        isGlobal: item.isGlobal,
      }
      this.isGlobal = this.model.isGlobal === 'Y'

      this.visible = true
    },
    async save(helpCode, content) {
      const scriptContent = (content || '').trim()
      if (!scriptContent || !helpCode) {
        return
      }
      const res = await postAction(this.url.checkMd5_url, {scriptContent: scriptContent, helpCode: helpCode})
      if (!res.result) {
        this.$message.warning(this.$t('m.components.label.shi-li-yi-cun-zai'))
        return
      }

      this.model = {
        helpName: '',
        helpDesc: '',
        scriptContent: scriptContent,
        helpCode: helpCode,
        orderNum: 1,
        isGlobal: 'N',
      }
      this.isGlobal = false

      this.visible = true
    },
  }
}
</script>
